//
//  PasswordChangeViewController.m
//  Kokozu
//
//  Created by Bismarrck on 11-7-18.
//  Copyright 2011年 Tsinghua University. All rights reserved.
//

#import "PasswordChangeViewController.h"
#import "UIViewController+BarItem.h"

@interface PasswordChangeViewController ()

- (void)passwordWillChange;
- (void)cancel;

@end

@implementation PasswordChangeViewController

@synthesize myOldPassword;
@synthesize myNewPassword;
@synthesize confirmPassword;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"background_light.png"]];
        self.title = @"修改密码";
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

- (void)dealloc
{
    [myOldPassword release];
    [myNewPassword release];
    [confirmPassword release];
    
    [super dealloc];
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    // Set the done button
    UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:@"确认修改"
                                                                   style:UIBarButtonItemStyleDone 
                                                                  target:self 
                                                                  action:@selector(passwordWillChange)];
    self.navigationItem.rightBarButtonItem = doneButton;
    [doneButton release];
    
    // Set the cancel button
    UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:@"取消修改" 
                                                                     style:UIBarButtonItemStyleBordered 
                                                                    target:self 
                                                                    action:@selector(cancel)];
    self.navigationItem.leftBarButtonItem = cancelButton;
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    
    myOldPassword = nil;
    myNewPassword = nil;
    confirmPassword = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


#pragma mark -
#pragma mark - Private methods

- (void)passwordWillChange
{
    NSString *newpw = [myNewPassword text];
    NSString *conpw = [confirmPassword text];
    
    if ([newpw isEqualToString:conpw]) {
        
        // update the data model
        
        // go back to account view
        [self dismissModalViewControllerAnimated:YES];
    }
    else {
        
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"请再次输入密码" 
                                                        message:@"新密码与确认密码不相同" 
                                                       delegate:nil 
                                              cancelButtonTitle:@"好的，再试一次" 
                                              otherButtonTitles:nil, nil];
        [alert show];
        [alert release];
    }
}

- (void)cancel
{
    [self dismissModalViewControllerAnimated:YES];
}

#pragma mark -
#pragma mark - Text Field Deleagte

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    [self resignFirstResponder];
}

@end
